package spt.mc.demo;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.SortedMap;
import java.util.TreeMap;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import spt.mc.demo.constant.McConstant;
import spt.mc.demo.entity.McInfo;
import spt.mc.demo.enums.APILiftTypeEnum;
import spt.mc.demo.enums.APIMcTypeEnum;
import spt.mc.demo.util.GenerateUtil;
import spt.mc.demo.util.MD5Util;

/**
 * 无纸化维保对接demo
 * @author spt
 *
 */
public class McDemo {

	
	public static void main(String[] args) throws UnsupportedEncodingException {
		//新增
		add();
		//修改更新
//		update();
	}

	//======新增参照这个方法
	@SuppressWarnings("unchecked")
	public static void add() throws UnsupportedEncodingException {
		McInfo mc=new McInfo()
				.setAppid(McConstant.APPID)
				.setAudtMan("审核人")
				.setAudtMark("审核意见")
				.setAudtPhone("审核人电话")
				.setAudtTime("2021-01-14 17:55:00")
				.setDevType(APILiftTypeEnum.TRACTIONCARGO.code())
				.setFirstMan("维保人1")
				.setFirstNum(null)
				.setFirstPhone("13111111111")
				.setLatitude(null)
				.setLongitude(null)
				//注意编码
				.setMcData(URLEncoder.encode(GenerateUtil.itemStr, "utf-8"))
				.setMcDay("2024-11-29") 
				//注意编码、维保图片的结构、使用https
				.setMcImg(URLEncoder.encode(GenerateUtil.imgStr, "utf-8"))
				.setMcMark(null)
				.setPlanDay("2024-11-29")
				.setSignAddr(null)
				.setSignImg(null)
				.setSignLatitude(null)
				.setSignLongitude(null)
				.setSignName("维保人1")
				.setSignTime("2024-11-29 17:55:00")
				.setSignUsc(1)
				.setSignOutTime("2024-11-29 19:55:00")
				.setSignOutAddr(null)
				.setSignOutLatitude(null)
				.setSignOutLongitude(null)
				.setSn("311010091202031318")
				.setTwoMan("维保人2")
				.setTwoNum(null)
				.setTwoPhone("13222222222")
				.setAudtSignImg(null)
				.setType(APIMcTypeEnum.HMONTHMC.code())
				.setPmCorp("物业公司")
				.setMcCorp("维保公司")
				.setUsc(1);
		
		
		//System.out.println(JSONUtil.toJsonStr(mc));
		SortedMap<String, Object> map=JSONUtil.toBean(JSONUtil.toJsonStr(mc), TreeMap.class);
		long timestamp=System.currentTimeMillis();
		String sign=MD5Util.createSign(map, McConstant.SECRET,timestamp);
		map.put("sign", sign);
		map.put("timestamp", timestamp);
		System.out.println(sign);
		//map.forEach((k,v)->{System.out.println(k+":"+v);});
		System.out.println(HttpUtil.post(McConstant.MC_API, map));
	}

	//============修改参照这个方法====================
	//调用修改接口时，id和appId是必传的，其它字段，需要修改则传，否则不需要传
	@SuppressWarnings("unchecked")
	public static void update() throws UnsupportedEncodingException {
		McInfo mc=new McInfo()
				.setId(1L)//*修改时必须要传ID
				.setAppid(McConstant.APPID)//*修改时必须要传AppId，不然鉴权通不过
				.setAudtMan("修改后的审核人")
				.setAudtMark("修改后的审核意见")
				.setAudtPhone("")
				.setAudtTime("2021-01-14 17:55:00");
		SortedMap<String, Object> map=JSONUtil.toBean(JSONUtil.toJsonStr(mc), TreeMap.class);
		long timestamp=System.currentTimeMillis();
		String sign=MD5Util.createSign(map, McConstant.SECRET,timestamp);
		map.put("sign", sign);
		map.put("timestamp", timestamp);
		System.out.println(sign);
		System.out.println(HttpUtil.post(McConstant.MC_API_UPD, map));
	}
}
